package com.konsung.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.konsung.bean.PatientBean;
import com.konsung.healthfile.R;
import com.konsung.ui.defineview.RoundImageView;
import com.konsung.util.IdCardUtil;
import com.konsung.util.UiUtils;
import com.synjones.bluetooth.BmpUtil;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 类功能：
 */

public class SwitchPatientListAdapter extends BaseAdapter {

    private final List<PatientBean> data;
    private final Context context;
    private final LayoutInflater layoutInflater;
    private List<String> deletePatient;
    private boolean isAllDeleteState = false;

    /**
     * 构造器
     * @param context 上下文
     * @param data 文本数据集
     */
    public SwitchPatientListAdapter(Context context, List<PatientBean> data) {
        this.data = data;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        deletePatient = new ArrayList<>();
    }

    @Override
    public int getCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_switch_patient_list, null);
            ButterKnife.bind(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final PatientBean patientBean = data.get(position);
        viewHolder.tvAge.setText(patientBean.getAge() + UiUtils.getString(R.string.year));
        viewHolder.tvName.setText(patientBean.getName());
        viewHolder.tvIdCardNumber.setText(patientBean.getIdcard());
        viewHolder.tvSex.setText(IdCardUtil.getSexFormDB(patientBean.getSex()));

        //获取居民头像
        Bitmap bitmap = BmpUtil.getBitmapByFileName(patientBean.getIdcard());
        Drawable icon = null;
        if (bitmap == null) {
            if (viewHolder.tvSex.getText().toString().equals(UiUtils.getString(R.string.man))) {
                icon = UiUtils.getDrawable(R.mipmap.pic_male);
            } else if (viewHolder.tvSex.getText().toString()
                    .equals(UiUtils.getString(R.string.woman))) {
                icon = UiUtils.getDrawable(R.mipmap.pic_female);
            }
            if (icon == null) {
                icon = UiUtils.getDrawable(R.mipmap.pic_unknow);
            }
            viewHolder.ivIcon.setImageDrawable(icon);
        } else {
            viewHolder.ivIcon.setImageBitmap(bitmap);
        }
        viewHolder.ivIcon.setVisibility(View.VISIBLE);

        return convertView;
    }



    /**
     * 获取deletePatient的值
     *
     * @return deletePatient deletePatient值
     */
    public List<String> getDeletePatient() {
        return deletePatient;
    }

    /**
     * 设置删除按钮是否全选
     * @param isChecked 是否全选
     */
    public void setAllDeleteState(boolean isChecked) {
        isAllDeleteState = isChecked;
        if (isChecked) {
            for (PatientBean bean : data) {
                deletePatient.clear();
                deletePatient.add(bean.getIdcard());
            }
        } else {
            deletePatient.clear();
        }
        notifyDataSetChanged();
    }

    /**
     * 视图容器
     */
    class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_id_card_number)
        TextView tvIdCardNumber;
        @BindView(R.id.tv_sex)
        TextView tvSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.iv_icon)
        RoundImageView ivIcon;
    }

}
